home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / SDKs / ScreenLight™ 1.0.1 / CodeExamples / Dots PPC-style / DotsModule.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-04  |  8.0 KB  |  351 lines  |  [TEXT/MPS ]

  1. /**********************************************************************************
  2.   DotsModule.c
  3.  
  4.  
  5.         DOCUMENTATION
  6.  
  7.              an example module for ScreenLight using PPC-style globals
  8.  
  9.  
  10.         MODULE CODE RESOURCES
  11.  
  12.             Here are the types we define for the kinds of Modules one
  13.         might produce.
  14.         
  15.             App: creator='SS-ß'
  16.  
  17.             68k Only: type='SS-m'
  18.                         code='SS-m' 0
  19.  
  20.             PPC Only: type='SS-p', 
  21.  
  22.             Fat Binary: type='SS-f', 
  23.                         code='SS-m' 0
  24.  
  25.  
  26.         HISTORY
  27.  
  28.  
  29.  
  30.         Copyright )WORK-IN_PROGRESS  noEsis Software Construction
  31. **********************************************************************************/
  32.  
  33.  
  34. //*********************************************************************************
  35. //                                                                  I N C L U D E                                                                             I N C L U D E S 
  36.  
  37. #include <A4Stuff.h>
  38. #include "ModuleRoutines.h"
  39.  
  40.  
  41. //*********************************************************************************
  42. //                                                                  F O R W A R D                                                                                        G L O B A L S
  43.  
  44.  
  45. OSErr AboutDefault( ModuleParamBlkPtr mpPtr);
  46. void GetNewPt( ModuleParamBlkPtr mpPtr);
  47.  
  48.  
  49. //*********************************************************************************
  50. //                                                                     G L O B A L S                                                                                         C O N S T A N T S
  51.  
  52. #define kPtCnt            16
  53. #define dMinDrawTime    5
  54.  
  55.     long            gLastDraw;
  56.     long            gCurHue;
  57.     long            gCurBrit;
  58.     long            gBritDir;        // 1,0
  59.     Point            gCurDir;        // h,v (-1,0,1)
  60.     Point            gPts[ kPtCnt];
  61.     Pattern            gWhitePat, gBlackPat;
  62.  
  63.  
  64. //*********************************************************************************
  65. //      main - MPW Linker wanted a routine named main.
  66. //*********************************************************************************
  67. OSErr main( ModuleParamBlkPtr mpPtr)
  68. {
  69.     OSErr    err;
  70.     
  71.     EnterCodeResource();
  72.     
  73.     err = ModOpen( mpPtr);
  74.     ExitCodeResource();
  75.     return err;
  76. }
  77.  
  78. OSErr ModOpen( ModuleParamBlkPtr mpPtr)
  79. {
  80.     RGBColor                tC;
  81.     short                    c;
  82.     Rect                    r;
  83.     OSErr                    err;
  84.  
  85.     EnterCodeResource();
  86.     
  87.     mpPtr->mProcs.drawProc = NewModDrawProc(ModDraw);
  88.     mpPtr->mProcs.closeProc = NewModCloseProc(ModClose);
  89.     mpPtr->mProcs.chngPrefsProc = NewModChangePrefsProc(ModChangePrefs);
  90.     mpPtr->mProcs.aboutProc = NewModAboutProc( AboutDefault);
  91.  
  92.     mpPtr->mStorage = 0;
  93.     
  94.     gLastDraw = TickCount();
  95.     gCurHue = IRandom( 0,360);
  96.     gCurBrit = 70;
  97.     gBritDir = 1;
  98.     for ( c=0;c<8;c++)
  99.         gWhitePat.pat[c] = 0x00;
  100.     for ( c=0;c<8;c++)
  101.         gBlackPat.pat[c] = 0xFF;
  102.     
  103.     gCurDir.h = gCurDir.v = 1;
  104.     
  105.     PenNormal();
  106.     BackColor( blackColor);
  107.     if ( GetPortPixDepth( mpPtr->mGraf) > 1) {
  108.         CalcHSBtoRGB( gCurHue, 80, gCurBrit, &tC);
  109.         RGBForeColor( &tC);
  110.     } else 
  111.          ForeColor( whiteColor);
  112.  
  113.     gPts[0].h = gPts[0].v = 20;
  114.     for (c=0;c<kPtCnt-1;c++) {
  115.         r.left = gPts[0].h;
  116.         r.top = gPts[0].v;
  117.         r.right = r.left + 4;
  118.         r.bottom = r.top + 4;
  119.         if ( IRandom(0,2)==1)
  120.             FillOval( &r, (Pattern*)gWhitePat.pat);
  121.         else 
  122.             FillOval( &r, (Pattern*)gBlackPat.pat);
  123.         GetNewPt( mpPtr);
  124.     }
  125.     r.left = gPts[0].h;
  126.     r.top = gPts[0].v;
  127.     r.right = r.left + 4;
  128.     r.bottom = r.top + 4;
  129.     if ( IRandom(0,6)==1)
  130.         FillOval( &r, (Pattern*)gWhitePat.pat);
  131.     else 
  132.         FillOval( &r, (Pattern*)gBlackPat.pat);
  133.  
  134.     ExitCodeResource();
  135.     return noErr;
  136. }
  137.  
  138. // Shift the existing points back, add a new Point at index 0
  139. //  01234567 -> n0123456
  140. void GetNewPt( ModuleParamBlkPtr mpPtr)
  141. {
  142.     Boolean                    bounce;
  143.     short                    c, extra, extDir;
  144.      
  145.     BlockMoveData( &gPts[0], &gPts[1], sizeof(Point)*(kPtCnt-1));
  146.  
  147.     // check if we need to change direction
  148.     bounce = false;
  149.     if ( gPts[0].h > mpPtr->mGraf->portRect.right) {
  150.         gCurDir.h = ( IRandom(0,2)==1)?(0):(-1);
  151.         if ( gCurDir.v == 0) {
  152.             c = IRandom( 0,3);    gCurDir.v = c - 1;
  153.         } 
  154.         bounce = true;
  155.     } else if ( gPts[0].h < mpPtr->mGraf->portRect.left) {
  156.         gCurDir.h = ( IRandom(0,2)==1)?(0):(1);
  157.         if ( gCurDir.v == 0) {
  158.             c = IRandom( 0,3);    gCurDir.v = c - 1;
  159.         } 
  160.         bounce = true;
  161.     }
  162.     
  163.     if ( gPts[0].v > mpPtr->mGraf->portRect.bottom) {
  164.         gCurDir.v = ( IRandom(0,2)==1)?(0):(-1);
  165.         if ( gCurDir.h == 0) {
  166.             c = IRandom( 0,3);    gCurDir.h = c - 1;
  167.         } 
  168.         bounce = true;
  169.     } else if ( gPts[0].v < mpPtr->mGraf->portRect.top) {
  170.         gCurDir.v = ( IRandom(0,2)==1)?(0):(1);
  171.         c = IRandom( 0,3);    gCurDir.h = c - 1;
  172.         if ( gCurDir.h == 0) {
  173.             c = IRandom( 0,3);    gCurDir.h = c - 1;
  174.         } 
  175.         bounce = true;
  176.     }
  177.     
  178.     if ( !bounce && IRandom(0,108*6)==1) {
  179.         do {
  180.             c = IRandom( 0,3);    gCurDir.h = c - 1;
  181.             c = IRandom( 0,3);    gCurDir.v = c - 1;
  182.         } while ( (gCurDir.h == 0 && gCurDir.v == 0));
  183.     } 
  184.     
  185.     // Now make new point
  186.     if ( !bounce && IRandom(0,2)==1) {
  187.         extra = true;
  188.         extDir = IRandom(0,2);
  189.     } else extra = false;
  190.  
  191.     if ( gCurDir.h == 1) {
  192.         gPts[0].h = gPts[1].h + 4;
  193.         if ( extra) {
  194.             if ( extDir == 1) gPts[0].v += IRandom( 1,6);
  195.              else gPts[0].v -= IRandom( 1,6);}
  196.     } else if ( gCurDir.h == -1) {
  197.         gPts[0].h = gPts[1].h - 4;
  198.         if ( extra) {
  199.             if ( extDir == 1) gPts[0].v -= IRandom( 1,6);
  200.              else  gPts[0].v += IRandom( 1,6);}
  201.     }
  202.  
  203.     if ( gCurDir.v == 1) {
  204.         gPts[0].v = gPts[1].v + 4;
  205.         if ( extra) {
  206.             if ( extDir == 1) gPts[0].h += IRandom( 1,6);
  207.              else gPts[0].h -= IRandom( 1,6);}
  208.     } else if ( gCurDir.v == -1) {
  209.         gPts[0].v = gPts[1].v - 4;
  210.         if ( extra) {
  211.             if ( extDir == 1) gPts[0].h -= IRandom( 1,6);
  212.              else gPts[0].h += IRandom( 1,6);}
  213.     }
  214.     
  215.  
  216.     return;
  217. }
  218.  
  219. //*************************************************
  220. OSErr ModDraw( ModuleParamBlkPtr mpPtr)
  221. {
  222.     RGBColor                tC;
  223.     Rect                    r;
  224.     long                    t;
  225.  
  226.      EnterCodeResource();
  227.  
  228.     if ( gLastDraw + dMinDrawTime > TickCount() )
  229.         goto done;;
  230.     
  231.     if ( IRandom( 1,6) == 1) {
  232.         gLastDraw ++;
  233.         goto done;
  234.     } 
  235.     if ( IRandom( 1,6) == 1) {
  236.         gLastDraw --;
  237.         goto done;
  238.     }
  239.     
  240.     PenNormal();
  241.     BackColor( blackColor);
  242.     r.left = gPts[kPtCnt-1].h;
  243.     r.top = gPts[kPtCnt-1].v;
  244.     r.right = r.left + 4;
  245.     r.bottom = r.top + 4;
  246.     FillOval( &r, (Pattern*)gBlackPat.pat);
  247.     
  248.     GetNewPt( mpPtr);
  249.     
  250.     if ( GetPortPixDepth( mpPtr->mGraf) > 1) {
  251.         CalcHSBtoRGB( gCurHue, 88, gCurBrit, &tC);
  252.         if ( IRandom(0,4) == 1) gCurHue++;
  253.         if ( IRandom(0,2) == 1) {
  254.             if ( gCurBrit == 100 ) {
  255.                 if ( IRandom(0,108*4) == 1) {
  256.                     if ( gBritDir == 1) gCurBrit++;
  257.                     else  gCurBrit--;
  258.                 }
  259.             } else {
  260.                 if ( IRandom(0,3) == 1) {
  261.                     if ( gBritDir == 1) gCurBrit++;
  262.                     else  gCurBrit--;
  263.                 }
  264.             }
  265.         }
  266.         RGBForeColor( &tC);
  267.     } else 
  268.          ForeColor( whiteColor);
  269.      if ( gCurHue > 360) gCurHue = 0;
  270.      if ( gCurBrit > 100) gBritDir = 0;
  271.       else if (  gCurBrit == 12) gBritDir = 1;
  272.  
  273.     r.left = gPts[0].h;
  274.     r.top = gPts[0].v;
  275.     r.right = r.left + 4;
  276.     r.bottom = r.top + 4;
  277.     if ( IRandom(0,6)==1)
  278.         FillOval( &r, (Pattern*)gWhitePat.pat);
  279.     else 
  280.         FillOval( &r, (Pattern*)gBlackPat.pat);
  281.     
  282.     gLastDraw = TickCount();
  283.  
  284. done:
  285.     ExitCodeResource();
  286.     return noErr;
  287. }
  288.  
  289. //*************************************************
  290. OSErr ModClose( ModuleParamBlkPtr mpPtr)
  291. {
  292.     return noErr;
  293. }
  294.  
  295.  
  296. //*************************************************
  297. OSErr ModChangePrefs( ModuleParamBlkPtr mpPtr)
  298. {
  299.     return noErr;
  300. }
  301.  
  302.  
  303. //*************************************************
  304. //    Sample About Procedure for a Module
  305.  
  306. OSErr AboutDefault( ModuleParamBlkPtr mpPtr)
  307. {
  308.     Rect            tmpR;
  309.     EventRecord        evt;
  310.     PicHandle        picH;
  311.     Rect            dR;
  312.     short            tWid, tHt, pWid, pHt;
  313.     short            offH, offV;
  314.     
  315.     EnterCodeResource();
  316.     
  317.     // GrafPort is set on entry
  318.     tmpR = mpPtr->mGraf->portRect;
  319.     
  320.     // let the ForeColor be as it may
  321.     //  just for fun..
  322.     PaintRect( &tmpR);
  323.     
  324.     picH = GetPicture( 128);
  325.     dR = (**picH).picFrame;
  326.     tWid = tmpR.right - tmpR.left;
  327.     tHt = tmpR.bottom - tmpR.top;
  328.     pWid = dR.right - dR.left;
  329.     pHt = dR.bottom - dR.top;
  330.     
  331.     offH = (tWid - pWid)/2;
  332.     offV = (tHt - pHt)/2;
  333.     OffsetRect(&dR,-dR.left,-dR.top);
  334.     OffsetRect(&dR, offH, offV);
  335.     
  336.     DrawPicture( picH, &dR);
  337.  
  338.     while ( !WaitNextEvent( mDownMask+keyDownMask, &evt, GetCaretTime(), 0))    
  339.         ; 
  340.         
  341.     ForeColor( blackColor);
  342.     PaintRect( &tmpR);
  343.     
  344.     ExitCodeResource();
  345.     return;
  346. }
  347.  
  348.  
  349. //**********************************************************************************
  350. //                                                      E N D   O F   L I S T I N G  
  351.